package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IDsClassService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程记录Controller
 *
 * @author tyro
 * @date 2025-03-23
 */
@RestController
@RequestMapping("/system/class")
public class DsClassController extends BaseController
{
    @Autowired
    private IDsClassService dsClassService;

    /**
     * 查询课程记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(DsClass dsClass)
    {
        startPage();
        List<DsClass> list = dsClassService.selectDsClassList(dsClass);
        return getDataTable(list);
    }

    /**
     * 查询课程记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:list')")
    @PostMapping("/listByTeacherId")
    public TableDataInfo listByTeacherId(@RequestBody DsClass dsClass)
    {
        startPage();
        List<DsClass> list = dsClassService.selectDsClassList(dsClass);
        return getDataTable(list);
    }

    /**
     * 导出课程记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:class:export')")
    @Log(title = "课程记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DsClass dsClass)
    {
        List<DsClass> list = dsClassService.selectDsClassList(dsClass);
        ExcelUtil<DsClass> util = new ExcelUtil<DsClass>(DsClass.class);
        util.exportExcel(response, list, "课程记录数据");
    }

    /**
     * 获取课程记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:class:query')")
    @GetMapping(value = "/{classId}")
    public AjaxResult getInfo(@PathVariable("classId") Long classId)
    {
        return success(dsClassService.selectDsClassByClassId(classId));
    }

    /**
     * 新增课程记录
     */
    @PreAuthorize("@ss.hasPermi('system:class:add')")
    @Log(title = "课程记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DsClass dsClass)
    {
        return toAjax(dsClassService.insertDsClass(dsClass));
    }

    /**
     * 修改课程记录
     */
    @PreAuthorize("@ss.hasPermi('system:class:edit')")
    @Log(title = "课程记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DsClass dsClass)
    {
        return toAjax(dsClassService.updateDsClass(dsClass));
    }

    /**
     * 删除课程记录
     */
    @PreAuthorize("@ss.hasPermi('system:class:remove')")
    @Log(title = "课程记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{classIds}")
    public AjaxResult remove(@PathVariable Long[] classIds)
    {
        return toAjax(dsClassService.deleteDsClassByClassIds(classIds));
    }

    /**
     * 获取课程学生名单
     */
    @GetMapping("/students/{classId}")
    public AjaxResult getStudentsByClassId(@PathVariable("classId") Long classId)
    {
        List<DsStudent> students = dsClassService.selectStudentsByClassId(classId);
        return success(students);
    }

    /**
     * 添加学生到课程
     */
    @Log(title = "添加课程学生", businessType = BusinessType.INSERT)
    @PostMapping("/students/add")
    public AjaxResult addStudentsToClass(@RequestBody AddStudentsRequest request)
    {
        return toAjax(dsClassService.addStudentsToClass(request.getClassId(), request.getStudentIds()));
    }

    /**
     * 从课程移除学生
     */
    @Log(title = "移除课程学生", businessType = BusinessType.DELETE)
    @PostMapping("/students/remove")
    public AjaxResult removeStudentsFromClass(@RequestBody RemoveStudentsRequest request)
    {
        return toAjax(dsClassService.removeStudentsFromClass(request.getClassId(), request.getStudentIds()));
    }

    /**
     * 根据班级名称批量添加学生到课程
     */
    @Log(title = "批量添加班级学生", businessType = BusinessType.INSERT)
    @PostMapping("/students/add-by-class")
    public AjaxResult addStudentsByClassName(@RequestBody AddStudentsByClassRequest request)
    {
        int count = dsClassService.addStudentsByClassName(request.getClassId(), request.getClassName());
        return success("成功添加 " + count + " 名学生到课程");
    }
}
